//
//  ZCNewTabBarController.m
//  zctx
//
//  Created by apple on 16/2/24.
//  Copyright © 2016年 sanliang. All rights reserved.
//

#import "ZCNewTabBarController.h"
#import "UIImage+YJImage.h"
#import "ZCTabBar.h"

#import "ZCRankingListVC.h"
#import "ZCEnrollVC.h"
#import "YYaoViewController.h"
#import "ZCActivitydescriptionVC.h"
@interface ZCNewTabBarController()<ZCTabBarDelegate>
@property (nonatomic,strong) ZCTabBar* jtabBar;
@property (nonatomic, strong) NSMutableArray *items;
@end

@implementation ZCNewTabBarController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    theAppdelegate.YJTabBar.hidesBottomBarWhenPushed = YES;
}

- (NSMutableArray *)items{
    if (_items == nil) {
        
        _items = [NSMutableArray array];
    }
    return _items;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initnavegation];
    self.navigationItem.title = @"食品安全小卫士评选";
    // 添加所有子控制器
    [self setUpAllChildViewController];
    [self setUpTabBar];
}

#pragma mark 导航栏
-(void)initnavegation{
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigation_bg"] forBarMetrics:UIBarMetricsDefault];
}

-(void)backButtonPressed{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setUpTabBar{
    // 自定义tabBar
    
    self.jtabBar = [[ZCTabBar alloc] initWithFrame:self.tabBar.frame];
    [self.jtabBar setBackgroundColor:[UIColor colorWithHexString:@"#FFFFFF"]];
    self.jtabBar.delegate = self;
    self.jtabBar.items = self.items;
    [self.view addSubview:self.jtabBar];
    // 移除系统的tabBar
    [self.tabBar removeFromSuperview];
}

- (void)setUpAllChildViewController{
    
    // 1
    ZCRankingListVC *harm = [[ZCRankingListVC alloc] init];
    [self setUpOneChildViewController:harm image:[UIImage imageNamed:@"info_tab_one_n"] selectedImage:[UIImage imageWithOriginalName:@"info_tab_one_p"] title:@"排名"];
    
    // 2
    ZCEnrollVC *home = [[ZCEnrollVC alloc] init];
    //    home.tabBarItem.badgeValue = @"2";
    [self setUpOneChildViewController:home image:[UIImage imageNamed:@"info_tab_two_n"] selectedImage:[UIImage imageWithOriginalName:@"info_tab_two_p"] title:@"报名"];
    
    ZCActivitydescriptionVC *actVC= [[ZCActivitydescriptionVC alloc]init];
    [self setUpOneChildViewController:actVC image:[UIImage imageNamed:@"info_tab_three_n"] selectedImage:[UIImage imageWithOriginalName:@"info_tab_three_p"] title:@"活动说明"];
    

//    // 3
//    YYaoViewController *involve = [[YYaoViewController alloc] init];
//    [self setUpOneChildViewController:involve image:[UIImage imageNamed:@"tab_problem_n"] selectedImage:[UIImage imageWithOriginalName:@"tab_problem_p"] title:@"活动说明"];

//    // 4
//    ZCMeVC *mevc = [[ZCMeVC alloc] init];
//    [self setUpOneChildViewController:mevc image:[UIImage imageNamed:@"tab_me_n"] selectedImage:[UIImage imageWithOriginalName:@"tab_me_p"] title:@"我的"];
}

#pragma mark - 当点击tabBar上的按钮调用
- (void)tabBar:(ZCTabBar *)tabBar didClickButton:(NSInteger)index{
    if (index == 0 && self.selectedIndex == index) {
        
    }
    self.selectedIndex = index;
}


- (void)setUpOneChildViewController:(UIViewController *)vc image:(UIImage *)image selectedImage:(UIImage *)selectedImage title:(NSString *)title{
    vc.title = title;
    vc.tabBarItem.image = image;
    vc.tabBarItem.selectedImage = selectedImage;
    
    // 保存tabBarItem模型到数组
    [self.items addObject:vc.tabBarItem];
    
//    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
//    [self addChildViewController:nav];
    
     [self addChildViewController:vc];
}



@end
